home *** CD-ROM | disk | FTP | other *** search
- /*
- * This software is provided for use AS-IS. No warranty is made as
- * to its operation or correctness.
- *
- * Written by David Dyer-Bennet
- * Terrabit Software
- * 4242 Minnehaha Ave S
- * Minneapolis, MN 55406
- *
- * Released to the public domain 22-Mar-87
- *
- * UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!ddb
- * UUCP: ...ihnp4!umn-cs!starfire!ddb
- * Fido: Sysop of Fido 14/341, The Terraboard, (612) 721-8967 3/12/24 24hrs
- * Telephone: (612) 721-8800 NOT 24 hrs! More like noon to midnight
- */
-
- /*
- * This program asks a question at the console and reads an answer, returning
- * zero if the answer was "yes" and non-zero if the answer was "no". So
- * far this is the same as ASK2, a standard pd utility. The added filip
- * here is that if no answer is received after a specified or default
- * interval, a specified or default default answer is returned. This is
- * useful in autoexec.bat for a system that often runs unattended (for
- * example, a bbs). You can set it up so that, if nobody is around, it
- * brings the bbs up when power comes on after a failure. However, if you
- * are present, you can tell it not to bring up the bbs. Similarly with
- * multi-taskers like DoubleDos.
- */
-
- /*
- * Revision history:
- *
- * Edit Date Who Description
- *
- * Version 1.0
- * 1 22-Mar-87 DD-B Initial creation
- */
-
-
- #include <stdio.h>
- #include <conio.h>
- #include <time.h>
-
- #define ANS_NO 1 /* status if no */
- #define ANS_YES 0 /* status if yes */
- #define DEFTIMEOUT 300 /* seconds */
- #define DEFANSWER ANS_NO /* Default answer on timeout */
-
- char szPrompt [128]; /* String to present to user */
- long tmWait; /* Time to wait in seconds */
- long tmTerminate; /* Time to terminate wait at if no answer */
- int bAnswer = DEFANSWER; /* Not changed if timeout */
-
- /*
- * Process the switch pointed to. First character is the switch indicator.
- */
-
- void ProcSwitch (sp)
- char *sp;
- {
- switch (tolower(*++sp)) {
- case 'd':
- tmWait = atol (++sp);
- break; /* case 'd' 'D' */
- case 'a':
- while (*++sp==' '||*sp=='\t')
- ; /* Skip leading white space */
- if (tolower (*sp) == 'y')
- bAnswer = ANS_YES;
- else if (tolower (*sp) == 'n')
- bAnswer = ANS_NO;
- else
- bomb ("-a option requires argument of y or n");
- break; /* case a */
- } /* switch *++sp */
- } /* ProcSwitch () */
-
- /*
- * Process another piece of the prompt string. If enclosed in quotes, strip.
- */
- ProcPrompt (pp)
- char *pp;
- {
- int i;
- i = strlen (pp) - 1;
- if (*pp == '"' && *(pp+i) == '"') {
- *(pp+i) = '\0'; /* Kill trailing quote */
- pp++; /* Kill leading quote */
- } /* if arg is quoted */
- if (szPrompt[0] != 0) strcat (szPrompt, " ");
- strcat (szPrompt, pp);
- } /* ProcPrompt () */
-
- /*
- * Terminate with extremem prejudice.
- */
-
- bomb (sp)
- char *sp;
- {
- cputs ("? Ask error: ");
- cputs (sp);
- cputs ("\r\n");
- cputs ("Usage: ask [-dseconds] [-adefaultanswer] prompt string\r\n");
- exit (2);
- } /* bomb () */
-
- main (ac, av)
- int ac;
- char *av[];
- {
- int ap, i;
- char c;
-
- for (ap=1; ap<ac; ap++) {
- if (*av[ap] == '-' || *av[ap] == '/')
- ProcSwitch (av[ap]);
- else
- ProcPrompt (av[ap]);
- } /* for ap */
- i = strlen (szPrompt)-1;
- if (szPrompt [i] != '?')
- szPrompt [++i] = '?';
- szPrompt [++i] = ' ';
- cputs (szPrompt);
-
- time (&tmTerminate); /* Get start time */
- if (tmWait) /* If delay (zero means forever) */
- tmTerminate += tmWait;
- else
- tmTerminate = 0;
-
- while (!tmWait || time (NULL) < tmTerminate) {
- if (kbhit()) {
- c = getch (); /* Get, no echo */
- if (tolower (c) == 'y') {
- putch (c); /* echo */
- bAnswer = ANS_YES;
- break;
- } else if (tolower (c) == 'n') {
- putch (c); /* echo */
- bAnswer = ANS_NO;
- break;
- } else
- putch ('\007'); /* Beep for bad characters */
- } /* if kbhit () */
- } /* while !tmWait || time < tmTerminate */
- cputs ("\r\n");
- cputs ("[Ask: answering ");
- cputs (bAnswer==ANS_YES?"YES":"NO");
- cputs ("]\r\n");
- exit (bAnswer);
- } /* main () */
-
- /* End of ask.c */
-